﻿/* 2013/1/10 */
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Cosmos.Media.Play
{
    /// <summary>
    /// Represents a collection of objects of PlayTree class.
    /// </summary>
    public class PlayTreeNodeCollection : IEnumerable<PlayTreeNode>, ICollection<PlayTreeNode>
    {
        private Collection<PlayTreeNode> _nodes;

        private PlayTreeNode _parentNode;

        /// <summary>
        /// Initializes a new instance of PlayTreeNodeCollection class.
        /// </summary>
        protected internal PlayTreeNodeCollection(PlayTreeNode parentNode)
        {
            _nodes = new Collection<PlayTreeNode>();

            _parentNode = parentNode;
        }

        public void Add(PlayTreeNode node)
        {
            node.ParentNode = _parentNode;
            _nodes.Add(node);
        }

        public void AddRange(IEnumerable<PlayTreeNode> nodes)
        {
            foreach (PlayTreeNode aNode in nodes)
            {
                aNode.ParentNode = _parentNode;
                _nodes.Add(aNode);
            }
        }

        public void Clear()
        {
            _nodes.Clear();
        }

        public bool Contains(PlayTreeNode node)
        {
            return _nodes.Contains(node);
        }

        public void CopyTo(PlayTreeNode[] array, int index)
        {
            _nodes.CopyTo(array, index);
        }

        public IEnumerator<PlayTreeNode> GetEnumerator()
        {
            return _nodes.GetEnumerator();
        }

        public int IndexOf(PlayTreeNode node)
        {
            return _nodes.IndexOf(node);
        }

        public void Insert(int index, PlayTreeNode node)
        {
            node.ParentNode = _parentNode;
            _nodes.Insert(index, node);
        }

        public bool Remove(PlayTreeNode node)
        {
            if (_nodes.Contains(node))
            {
                node.ParentNode = null;
            }
            return _nodes.Remove(node);
        }

        public void RemoveAt(int index)
        {
            if (index >= 0 && index < _nodes.Count)
            {
                PlayTreeNode item = _nodes[index];
                item.ParentNode = null;
            }
            _nodes.RemoveAt(index);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)_nodes.GetEnumerator();
        }

        public int Count
        {
            get
            {
                return _nodes.Count;
            }
        }

        bool ICollection<PlayTreeNode>.IsReadOnly
        {
            get
            {
                return ((ICollection<PlayTreeNode>)_nodes).IsReadOnly;
            }
        }

        public PlayTreeNode this[int index]
        {
            get
            {
                return _nodes[index];
            }

            set
            {
                _nodes[index] = value;
            }
        }

        public PlayTreeNode ParentNode
        {
            get
            {
                return _parentNode;
            }
        }

        protected Collection<PlayTreeNode> Nodes
        {
            get
            {
                return _nodes;
            }

            set
            {
                _nodes = value;
            }
        }
    }
}
